/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.web.portlet.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.genia.toolbox.basics.exception.BundledException;
import com.genia.toolbox.basics.exception.technical.TechnicalIOException;
import com.genia.toolbox.web.portlet.bean.Portlet;
import com.genia.toolbox.web.portlet.visitor.impl.RenderingPortletVisitor;

/**
 * Spring controller to display Portlets.
 */
public abstract class AbstractPortletController
    extends AbstractController
{
  /**
   * the portlet to display.
   */
  private Portlet portlet;

  /**
   * the character encoding of the response.
   */
  private String characterEncoding = null;

  /**
   * the content-type of the response.
   */
  private String contentType = "text/html";



  /**
   * the main method of the Controller. It used a
   * <code>RenderingPortletVisitor</code> to render the <code>Portlet</code>.
   * 
   * @param request
   *          current HTTP request
   * @param response
   *          current HTTP response
   * @throws BundledException
   *           when an error occurred
   * @return always <code>null</code> as the rendering is made by the
   *         <code>RenderingPortletVisitor</code>
   */
  @Override
  protected ModelAndView handleRequestInternal(final HttpServletRequest request, final HttpServletResponse response)
      throws BundledException
  {

    response.setContentType(getContentType());
    if (getCharacterEncoding() != null) {
      response.setCharacterEncoding(getCharacterEncoding());
    }

    final RenderingPortletVisitor visitor = createRenderingPortletVisitor();
    visitor.setRequest(request);
    visitor.setResponse(response);
    getPortlet().visit(visitor);
    try {
      if (!response.isCommitted()) {
        response.getOutputStream().write(visitor.getOutput());
      }
    }
    catch (final IOException e) {
      throw new TechnicalIOException(e);
    }
    return null;
  }



  /**
   * factory method for the {@link RenderingPortletVisitor}.
   * 
   * @return a new {@link RenderingPortletVisitor}
   */
  public abstract RenderingPortletVisitor createRenderingPortletVisitor();



  /**
   * getter for the portlet property.
   * 
   * @return the portlet
   */
  public Portlet getPortlet()
  {
    return portlet;
  }



  /**
   * setter for the portlet property.
   * 
   * @param portlet
   *          the portlet to set
   */
  public void setPortlet(final Portlet portlet)
  {
    this.portlet = portlet;
  }



  /**
   * getter for the characterEncoding property.
   * 
   * @return the characterEncoding
   */
  public String getCharacterEncoding()
  {
    return characterEncoding;
  }



  /**
   * setter for the characterEncoding property.
   * 
   * @param characterEncoding
   *          the characterEncoding to set
   */
  public void setCharacterEncoding(final String characterEncoding)
  {
    this.characterEncoding = characterEncoding;
  }



  /**
   * getter for the contentType property.
   * 
   * @return the contentType
   */
  public String getContentType()
  {
    return contentType;
  }



  /**
   * setter for the contentType property.
   * 
   * @param contentType
   *          the contentType to set
   */
  public void setContentType(final String contentType)
  {
    this.contentType = contentType;
  }

}
